//
//  LYPayFinshViewController.m
//  artist
//
//  Created by Jay_Apple on 16/4/11.
//  Copyright © 2016年 viviyoo. All rights reserved.
//

#import "LYPayFinshViewController.h"
#import "LYOrderDetailModel.h"
#import "NSDate+Utils.h"
#import "LYAssesmentViewController.h"
#import <CoreImage/CoreImage.h>
@interface LYPayFinshViewController ()
@property (nonatomic, strong) LYOrderDetailModel *model;

@property (weak, nonatomic) IBOutlet UILabel *orzTitle;
@property (weak, nonatomic) IBOutlet UILabel *address;
@property (weak, nonatomic) IBOutlet UILabel *BMobile;
@property (weak, nonatomic) IBOutlet UILabel *time;
@property (weak, nonatomic) IBOutlet UILabel *code;
@property (weak, nonatomic) IBOutlet UILabel *status;

@property (weak, nonatomic) IBOutlet UIImageView *codeImage;
@property (weak, nonatomic) IBOutlet UILabel *orderNum;
@property (weak, nonatomic) IBOutlet UILabel *payTime;
@property (weak, nonatomic) IBOutlet UILabel *price;

@property (strong, nonatomic) UIBarButtonItem *rightItem;

@end

@implementation LYPayFinshViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setUpNav];

    [self getOrderDetailData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark -懒加载
- (UIBarButtonItem *)rightItem{
    
    if (_rightItem == nil) {
        _rightItem = [[UIBarButtonItem alloc] initWithTitle:@"评价" style:UIBarButtonItemStyleDone target:self action:@selector(comment)];
    }
    return _rightItem;

}



#pragma mark - 初始化

- (void)setUpNav{

    self.navigationItem.title = @"订单详情";

}

- (void)comment{

    LYAssesmentViewController *commentVC = [[LYAssesmentViewController alloc] init];
    commentVC.ID = self.model.data_id;
    commentVC.type = CommentTypeClass;
    commentVC.order_sn = self.order_sn;
    [self.navigationController pushViewController:commentVC animated:YES];
    

}

- (void)getOrderDetailData{
    
    [MBProgressHUD showMessage:nil];
    [LYRequestTool orderDetailWithToken:LYToken order_sn:self.order_sn success:^(LYOrderDetailModel *model) {
        [MBProgressHUD hideHUD];
        self.model = model;
        
    } failure:^(NSString *code, NSString *message) {
        [MBProgressHUD hideHUD];
    }];
    
}


- (void)setModel:(LYOrderDetailModel *)model{
    
    _model = model;
    
    self.orzTitle.text = [NSString stringWithFormat:@"机构：%@",_model.title];
    
    //地址
    NSString *address = [NSString stringWithFormat:@"上课地点：%@%@%@%@",_model.course_info.province,_model.course_info.city,_model.course_info.area,_model.course_info.address];
    self.address.text = address;
    //价钱
    self.price.text = [NSString stringWithFormat:@"总价：¥%@",_model.order_money];
    //电话
    self.BMobile.text = [NSString stringWithFormat:@"电话：%@",_model.course_info.mobile];
    //有效期
    self.time.text = [NSString stringWithFormat:@"有效期%@",_model.course_info.end_date];
    //验证码
    self.code.text = [NSString stringWithFormat:@"验证码%@",_model.verify_code];
    //状态
//    if ( [_model.verify_time isEqualToString:@"0"]) {
//        self.status.text = @"未消费";
//        self.navigationItem.rightBarButtonItem = nil;
//        
//    }else{
//        
//        self.status.text = @"已消费";
//        self.navigationItem.rightBarButtonItem = self.rightItem;
//    }
    self.navigationItem.rightBarButtonItem = nil;
    
    switch (_model.status) {
        case orderStatusPay:
            self.status.text = @"已支付";
            break;
            
        case orderStatusNoPay:
            self.status.text = @"未支付";
            break;
            
        case orderStatusUsed:
            self.status.text = @"已验证";
            self.navigationItem.rightBarButtonItem = self.rightItem;
            break;
            
        case orderStatusCancel:
            self.status.text = @"已取消";
            break;
            
        case orderStatusHanding:
            self.status.text = @"退款中";
            break;
            
        case orderStatusRefund:
            self.status.text = @"已退款";
            break;
            
        case orderStatusApplyRefund:
            self.status.text = @"申请退款";
            break;
    }

    
    //订单号
    self.orderNum.text = [NSString stringWithFormat:@"订单号：%@",_model.order_sn];
    //付款时间
    NSDate *date = [NSDate dateFromTimeStampString:_model.create_time];
    self.payTime.text = [NSString stringWithFormat:@"付款时间：%@",[date string]];
    //二维码
    NSString *dataStr = [NSString stringWithFormat:@"%@#%@",_model.verify_code,_model.order_sn];
    self.codeImage.image = [self createCodeImageWithStr:dataStr];
}


- (UIImage *)createCodeImageWithStr:(NSString *)dataString
{
    // 1.创建滤镜对象
    CIFilter *filter = [CIFilter filterWithName:@"CIQRCodeGenerator"];
    
    // 2.设置相关的信息
    [filter setDefaults];
    
    // 3.设置二维码的数据
    NSData *data = [dataString dataUsingEncoding:NSUTF8StringEncoding];
    [filter setValue:data forKeyPath:@"inputMessage"];
    
    
    // 4.获取输出的图片
    CIImage *outputImage = [filter outputImage];
    
    // 5.设置到imageView上即可
    return [self createNonInterpolatedUIImageFormCIImage:outputImage withSize:220];
}

/**
 *  根据CIImage生成指定大小的UIImage
 *
 *  @param image CIImage
 *  @param size  图片宽度
 */
- (UIImage *)createNonInterpolatedUIImageFormCIImage:(CIImage *)image withSize:(CGFloat) size
{
    CGRect extent = CGRectIntegral(image.extent);
    CGFloat scale = MIN(size/CGRectGetWidth(extent), size/CGRectGetHeight(extent));
    
    // 1.创建bitmap;
    size_t width = CGRectGetWidth(extent) * scale;
    size_t height = CGRectGetHeight(extent) * scale;
    CGColorSpaceRef cs = CGColorSpaceCreateDeviceGray();
    CGContextRef bitmapRef = CGBitmapContextCreate(nil, width, height, 8, 0, cs, (CGBitmapInfo)kCGImageAlphaNone);
    CIContext *context = [CIContext contextWithOptions:nil];
    CGImageRef bitmapImage = [context createCGImage:image fromRect:extent];
    CGContextSetInterpolationQuality(bitmapRef, kCGInterpolationNone);
    CGContextScaleCTM(bitmapRef, scale, scale);
    CGContextDrawImage(bitmapRef, extent, bitmapImage);
    
    // 2.保存bitmap到图片
    CGImageRef scaledImage = CGBitmapContextCreateImage(bitmapRef);
    CGContextRelease(bitmapRef);
    CGImageRelease(bitmapImage);
    return [UIImage imageWithCGImage:scaledImage];
}


@end
